import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { Relax } from 'iflux2'
import { transformURL } from 'util/image'
import _ from 'util'
import noop from 'util/noop'

import { Popconfirm, Modal } from 'antd'
import GaUtil from "util/ga-util";

@Relax
export default class NewsItem extends Component {

    static defaultProps = {
        item: '',
        _changeNews: noop,
        _delNews: noop,
    };
    render() {
        const item = this.props.item;

        return (
            <li>
                <div className="createTime">{_.getTime(item.get('update_time') || item.get('updatedAt'))}</div>
                {
                    (item.getIn(['content', 'news_item']) || item.getIn(['articles'])).map((item1, index1) => {
                        if (index1 == 0) {
                            return (
                                <div className="singleNews">
                                    <h1>{item1.get('title')}</h1>
                                    <img src={transformURL(item1.get('thumb_url'))} alt="" />
                                    <p>{item1.get('digest')}</p>
                                </div>)
                        }
                        return (
                            <div className="tinyNews">
                                <span>{item1.get('title')}</span>
                                <img src={transformURL(item1.get('thumb_url'))} alt="" />
                            </div>)
                    })
                }
                <div className="btns">
                    <div onClick={this._editNews} className="btn normal">编辑</div>
                    <div className="btn private" onClick={() => this._del(item.get('id') || item.get('media_id'))}>删除</div>
                </div>
            </li>)
    }

    //将当前这条图文设为编辑状态
    _editNews = () => {
        const { item, _changeNews } = this.props
        _changeNews(item)
    }

    //删除整条图文
    _del = (id) => {
        Modal.confirm({
            title: '你是否确定删除该图文',
            content: '删除后将无法找回图文',
            okText: '确认',
            cancelText: '取消',
            onOk: () => this.props._delNews(id)
        });
        GaUtil.addEvent({eventAction: this.props.item.get('media_id') ? "删除图文" : "删除高级图文", eventLabel: "图文素材"})

    }
}